﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SacNetUG.Controllers;
using SacNetUG.Models;
using SacNetUG.Models.ViewModels.Home;
using SacNetUG.Tests.Fakes;

namespace SacNetUG.Tests.Controllers
{
    [TestClass]
    public class HomeControllerTest
    {
        private ISacNetUGRepository repository;
        private MyFakeDatabase db;
        private FakeDataContextWrapper dataContext;
        private HomeController controller;

        [TestInitialize]
        public void SetUp()
        {
            db = new MyFakeDatabase();
            dataContext = new FakeDataContextWrapper(db);
            repository = new SacNetUGRepository(dataContext);
            controller = new HomeController(repository);

            controller.SetFakeControllerContext();
        }

        [TestMethod]
        public void IndexReturnsEvents()
        {
            // Act
            var result = controller.Index() as ViewResult;

            // Assert
            var events = result.ViewData.Model as IEnumerable<IndexViewModel>;
            Assert.AreEqual(1, events.ToList().Count);
        }

        [TestMethod]
        public void NotFoundReturnsMissingKids()
        {
            // Arrange
            var missingKids = new XmlDocument();
            missingKids.LoadXml(GetMissingKids());
            repository.MissingKids = missingKids;

            // Act
            var result = controller.NotFound() as ViewResult;

            // Assert
            var kids = result.ViewData.Model as List<MissingKid>;
            Assert.AreEqual(1, kids.Count);
        }

        private string GetMissingKids()
        {
            return @"<locations><maplocation zoom=""4"" state_long=""-94.130859"" state_lat=""40.313043""/><location id=""Larsen"" firstname=""David"" lastname=""Larsen"" picture=""img width=160 target=_new src="" picture2="""" medpic=""img width=60 border=0 target=_new src="" smallpic=""img width=30 border=0 target=_new src="" policenum=""1-509-456-2236"" policeadd=""Spokane Police Department  (Washington)"" policenum2="""" policeadd2="""" st="" WA"" city=""SPOKANE"" missing=""02/25/2010"" status=""Endangered Missing"" age=""8"" url=""1144166"" lat=""47.6588888888889"" lng=""-117.425""/></locations>";
        }
    }
}
